package com.shundr.shipper.common;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.shundr.shipper.GuideActivity;
import com.shundr.shipper.R;
import com.shundr.shipper.base.BaseActivity;
import com.shundr.shipper.common.http.CommonRequest;
import com.shundr.shipper.common.model.VersionInfo;
import com.shundr.shipper.common.util.ToastHelper;
import com.shundr.shipper.common.view.DialodUpdateApp;
import com.shundr.shipper.common.view.DialogCall;
import com.shundr.shipper.frame.util.GetVersionUtil;

/**
 * 关于界面
 * AboutActivity
 * 
 * @author larry 2015-5-19 下午8:52:15 
 * @version 1.0.0
 *
 */
public class AboutActivity extends BaseActivity implements OnClickListener {

	private TextView tvVersionName;
	private Button btnIntroduction;
	private Button btnCallService;
	private Button btnQr;
	private Button btnVersion;
	
	@SuppressLint("HandlerLeak")
	Handler mHandler=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			String description="";
			try {
				switch (msg.what) {
				case CommonRequest.GET_APPSYS_INFO_UPDATE:
					VersionInfo version = (VersionInfo)msg.obj;
					if(version == null)
						return;
					DialodUpdateApp dialodUpdateApp=new DialodUpdateApp(mContext, version);
					dialodUpdateApp.show();
					break;
				case CommonRequest.GET_APPSYS_INFO_NO_UPDATE:
					description=(String)msg.obj;
					ToastHelper.toastShort(mContext, description);
					break;
				case CommonRequest.GET_APPSYS_INFO_FAILED:
					description=(String)msg.obj;
					ToastHelper.toastShort(mContext, description);
					break;
				case CommonRequest.REQUEST_FAILED:
					description=(String)msg.obj;
					ToastHelper.toastShort(mContext, description);
					break;
				default:
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			super.handleMessage(msg);
		}
		
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_about_shundr);

		tvVersionName = (TextView) findViewById(R.id.tv_version_name);
		btnIntroduction = (Button) findViewById(R.id.btn_about_introduce);
		btnCallService=(Button)findViewById(R.id.btn_about_call);
		btnVersion = (Button)findViewById(R.id.btn_check_version);
		btnQr = (Button)findViewById(R.id.btn_qr_code);
		
		btnIntroduction.setOnClickListener(this);
		btnCallService.setOnClickListener(this);
		btnVersion.setOnClickListener(this);
		btnQr.setOnClickListener(this);
		tvVersionName.setText("V " + GetVersionUtil.getVersionName(getApplicationContext()));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_check_version:
			checkVersion();
			break;
		case R.id.btn_about_introduce:
            Intent intent = new Intent(mContext, GuideActivity.class);
            intent.putExtra("flag", true);
            startActivity(intent);
			break;
		case R.id.btn_about_call:
			DialogCall callDialog = new DialogCall(mContext);
			callDialog.show();
			break;
		case R.id.btn_qr_code:
			startActivity(new Intent(mContext, QrActivity.class));
			break;
		default:
			break;
		}
	}
	
	/**
	 * 检查版本更新
	 */
	private void checkVersion(){	
		try {
			int VersionCode=GetVersionUtil.getVersionCode(getApplicationContext());
			int androidVersion = Integer.valueOf(android.os.Build.VERSION.SDK_INT);
			new CommonRequest(mContext, mHandler).getAppSysInfo(VersionCode+"", androidVersion+"");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onDestroy() {
		try {
			mHandler.removeCallbacksAndMessages(null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		super.onDestroy();
	}

}
